package com.weathereport.dao;

import java.util.ArrayList;
import java.util.List;

import com.weathereport.R;
import com.weathereport.entity.Forecast;
import com.weathereport.entity.Local;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.Time;

public class WeatherDAO extends SQLiteOpenHelper{
	
	private static final String DBNAME 							= "local_database";
	private static final String DAO_TABLE_LOCAL 				= "local";	
	private static final String LOCAL_ID 						= "id";	
	private static final String LOCAL_NAME 						= "name";	
	private static final String LOCAL_LATITUDE 					= "latitude";	
	private static final String LOCAL_LONGITUDE 				= "longitude";
	
	private final static String FORECAST_DAY1	 				= "d1";
	private final static String FORECAST_TEMPNOW1	 			= "tn1";
	private final static String FORECAST_WINDNOW1	 			= "wn1";
	private final static String FORECAST_HUMIDITYNOW1	 		= "hn1";
	private final static String FORECAST_RAINNOW1	 			= "rn1";
	private final static String FORECAST_WEATHER1 				= "w1";	
	private final static String FORECAST_LOWTEMP1				= "lt1";	
	private final static String FORECAST_HIGHTEMP1 				= "ht1";
	private final static String FORECAST_RAIN1	 				= "r1";
	private final static String FORECAST_SNOW1	 				= "s1";
	private final static String FORECAST_WIND1	 				= "aw1";
	private final static String FORECAST_HUMIDITY1	 			= "ah1";
	
	private final static String FORECAST_DAY2	 				= "d2";
	private final static String FORECAST_WEATHER2 				= "w2";	
	private final static String FORECAST_LOWTEMP2				= "lt2";	
	private final static String FORECAST_HIGHTEMP2 				= "ht2";
	private final static String FORECAST_RAIN2	 				= "r2";
	private final static String FORECAST_SNOW2	 				= "s2";
	private final static String FORECAST_WIND2	 				= "aw2";
	private final static String FORECAST_HUMIDITY2	 			= "ah2";
	
	private final static String FORECAST_DAY3	 				= "d3";
	private final static String FORECAST_WEATHER3 				= "w3";	
	private final static String FORECAST_LOWTEMP3				= "lt3";	
	private final static String FORECAST_HIGHTEMP3 				= "ht3";
	private final static String FORECAST_RAIN3	 				= "r3";
	private final static String FORECAST_SNOW3	 				= "s3";
	private final static String FORECAST_WIND3	 				= "aw3";
	private final static String FORECAST_HUMIDITY3	 			= "ah3";
	
	private final static String FORECAST_DAY4	 				= "d4";
	private final static String FORECAST_WEATHER4 				= "w4";	
	private final static String FORECAST_LOWTEMP4				= "lt4";	
	private final static String FORECAST_HIGHTEMP4 				= "ht4";
	private final static String FORECAST_RAIN4	 				= "r4";
	private final static String FORECAST_SNOW4	 				= "s4";
	private final static String FORECAST_WIND4	 				= "aw4";
	private final static String FORECAST_HUMIDITY4	 			= "ah4";
	
	private final static String FORECAST_DAY5	 				= "d5";
	private final static String FORECAST_WEATHER5 				= "w5";	
	private final static String FORECAST_LOWTEMP5				= "lt5";	
	private final static String FORECAST_HIGHTEMP5 				= "ht5";
	private final static String FORECAST_RAIN5	 				= "r5";
	private final static String FORECAST_SNOW5	 				= "s5";
	private final static String FORECAST_WIND5	 				= "aw5";
	private final static String FORECAST_HUMIDITY5	 			= "ah5";
	
	private final static String FORECAST_DAY6	 				= "d6";
	private final static String FORECAST_WEATHER6 				= "w6";	
	private final static String FORECAST_LOWTEMP6				= "lt6";	
	private final static String FORECAST_HIGHTEMP6 				= "ht6";
	private final static String FORECAST_RAIN6	 				= "r6";
	private final static String FORECAST_SNOW6	 				= "s6";
	private final static String FORECAST_WIND6	 				= "aw6";
	private final static String FORECAST_HUMIDITY6	 			= "ah6";
	
	private final static String FORECAST_DAY7	 				= "d7";
	private final static String FORECAST_WEATHER7 				= "w7";	
	private final static String FORECAST_LOWTEMP7				= "lt7";	
	private final static String FORECAST_HIGHTEMP7 				= "ht7";
	private final static String FORECAST_RAIN7	 				= "r7";
	private final static String FORECAST_SNOW7	 				= "s7";
	private final static String FORECAST_WIND7	 				= "aw7";
	private final static String FORECAST_HUMIDITY7	 			= "ah7";
	
	private final static String FORECAST_DAY8	 				= "d8";
	private final static String FORECAST_WEATHER8 				= "w8";	
	private final static String FORECAST_LOWTEMP8				= "lt8";	
	private final static String FORECAST_HIGHTEMP8 				= "ht8";
	private final static String FORECAST_RAIN8	 				= "r8";
	private final static String FORECAST_SNOW8	 				= "s8";
	private final static String FORECAST_WIND8	 				= "aw8";
	private final static String FORECAST_HUMIDITY8	 			= "ah8";
	
	private final static String FORECAST_DAY9	 				= "d9";
	private final static String FORECAST_WEATHER9 				= "w9";	
	private final static String FORECAST_LOWTEMP9				= "lt9";	
	private final static String FORECAST_HIGHTEMP9 				= "ht9";
	private final static String FORECAST_RAIN9	 				= "r9";
	private final static String FORECAST_SNOW9	 				= "s9";
	private final static String FORECAST_WIND9	 				= "aw9";
	private final static String FORECAST_HUMIDITY9	 			= "ah9";
	
	private final static String FORECAST_DAY10	 				= "d10";
	private final static String FORECAST_WEATHER10				= "w10";	
	private final static String FORECAST_LOWTEMP10				= "lt10";	
	private final static String FORECAST_HIGHTEMP10 			= "ht10";
	private final static String FORECAST_RAIN10	 				= "r10";
	private final static String FORECAST_SNOW10	 				= "s10";
	private final static String FORECAST_WIND10	 				= "aw10";
	private final static String FORECAST_HUMIDITY10	 			= "ah10";
	
	private final static int DBVERSION							= 1;	
	private final static String[] COLUMNS_LOCAL 				= {LOCAL_ID, LOCAL_NAME, LOCAL_LATITUDE, LOCAL_LONGITUDE, FORECAST_DAY1, FORECAST_TEMPNOW1, FORECAST_WINDNOW1, FORECAST_HUMIDITYNOW1, FORECAST_RAINNOW1, FORECAST_WEATHER1, FORECAST_LOWTEMP1, FORECAST_HIGHTEMP1, FORECAST_RAIN1, FORECAST_SNOW1, FORECAST_WIND1, FORECAST_HUMIDITY1,
		FORECAST_DAY2, FORECAST_WEATHER2, FORECAST_LOWTEMP2, FORECAST_HIGHTEMP2, FORECAST_RAIN2, FORECAST_SNOW2, FORECAST_WIND2, FORECAST_HUMIDITY2, FORECAST_DAY3, FORECAST_WEATHER3, FORECAST_LOWTEMP3, FORECAST_HIGHTEMP3, FORECAST_RAIN3, FORECAST_SNOW3, FORECAST_WIND3, FORECAST_HUMIDITY3, FORECAST_DAY4, FORECAST_WEATHER4, FORECAST_LOWTEMP4, FORECAST_HIGHTEMP4, FORECAST_RAIN4, FORECAST_SNOW4, FORECAST_WIND4, FORECAST_HUMIDITY4,
		FORECAST_DAY5, FORECAST_WEATHER5, FORECAST_LOWTEMP5, FORECAST_HIGHTEMP5, FORECAST_RAIN5, FORECAST_SNOW5, FORECAST_WIND5, FORECAST_HUMIDITY5, FORECAST_DAY6, FORECAST_WEATHER6, FORECAST_LOWTEMP6, FORECAST_HIGHTEMP6, FORECAST_RAIN6, FORECAST_SNOW6, FORECAST_WIND6, FORECAST_HUMIDITY6, FORECAST_DAY7, FORECAST_WEATHER7, FORECAST_LOWTEMP7, FORECAST_HIGHTEMP7, FORECAST_RAIN7, FORECAST_SNOW7, FORECAST_WIND7, FORECAST_HUMIDITY7,
		FORECAST_DAY8, FORECAST_WEATHER8, FORECAST_LOWTEMP8, FORECAST_HIGHTEMP8, FORECAST_RAIN8, FORECAST_SNOW8, FORECAST_WIND8, FORECAST_HUMIDITY8, FORECAST_DAY9, FORECAST_WEATHER9, FORECAST_LOWTEMP9, FORECAST_HIGHTEMP9, FORECAST_RAIN9, FORECAST_SNOW9, FORECAST_WIND9, FORECAST_HUMIDITY9, FORECAST_DAY10, FORECAST_WEATHER10, FORECAST_LOWTEMP10, FORECAST_HIGHTEMP10, FORECAST_RAIN10, FORECAST_SNOW10, FORECAST_WIND10, FORECAST_HUMIDITY10};
	private final static String[] FORECAST1						= {FORECAST_DAY1, FORECAST_TEMPNOW1, FORECAST_WINDNOW1, FORECAST_HUMIDITYNOW1, FORECAST_RAINNOW1, FORECAST_WEATHER1, FORECAST_LOWTEMP1, FORECAST_HIGHTEMP1, FORECAST_RAIN1, FORECAST_SNOW1};
	private final static String[] FORECAST2						= {FORECAST_DAY2, FORECAST_WEATHER2, FORECAST_LOWTEMP2, FORECAST_HIGHTEMP2, FORECAST_RAIN2, FORECAST_SNOW2, FORECAST_WIND2, FORECAST_HUMIDITY2};
	private final static String[] FORECAST3						= {FORECAST_DAY3, FORECAST_WEATHER3, FORECAST_LOWTEMP3, FORECAST_HIGHTEMP3, FORECAST_RAIN3, FORECAST_SNOW3, FORECAST_WIND3, FORECAST_HUMIDITY3};
	private final static String[] FORECAST4						= {FORECAST_DAY4, FORECAST_WEATHER4, FORECAST_LOWTEMP4, FORECAST_HIGHTEMP4, FORECAST_RAIN4, FORECAST_SNOW4, FORECAST_WIND4, FORECAST_HUMIDITY4};
	private final static String[] FORECAST5						= {FORECAST_DAY5, FORECAST_WEATHER5, FORECAST_LOWTEMP5, FORECAST_HIGHTEMP5, FORECAST_RAIN5, FORECAST_SNOW5, FORECAST_WIND5, FORECAST_HUMIDITY5};
	private final static String[] FORECAST6						= {FORECAST_DAY6, FORECAST_WEATHER6, FORECAST_LOWTEMP6, FORECAST_HIGHTEMP6, FORECAST_RAIN6, FORECAST_SNOW6, FORECAST_WIND6, FORECAST_HUMIDITY6};
	private final static String[] FORECAST7						= {FORECAST_DAY7, FORECAST_WEATHER7, FORECAST_LOWTEMP7, FORECAST_HIGHTEMP7, FORECAST_RAIN7, FORECAST_SNOW7, FORECAST_WIND7, FORECAST_HUMIDITY7};
	private final static String[] FORECAST8						= {FORECAST_DAY8, FORECAST_WEATHER8, FORECAST_LOWTEMP8, FORECAST_HIGHTEMP8, FORECAST_RAIN8, FORECAST_SNOW8, FORECAST_WIND8, FORECAST_HUMIDITY8};
	private final static String[] FORECAST9						= {FORECAST_DAY9, FORECAST_WEATHER9, FORECAST_LOWTEMP9, FORECAST_HIGHTEMP9, FORECAST_RAIN9, FORECAST_SNOW9, FORECAST_WIND9, FORECAST_HUMIDITY9};
	private final static String[] FORECAST10					= {FORECAST_DAY10, FORECAST_WEATHER10, FORECAST_LOWTEMP10, FORECAST_HIGHTEMP10, FORECAST_RAIN10, FORECAST_SNOW10, FORECAST_WIND10, FORECAST_HUMIDITY10};
	private final static String[] FORECASTDAYS					= {FORECAST_DAY1, FORECAST_DAY2, FORECAST_DAY3, FORECAST_DAY4, FORECAST_DAY5, FORECAST_DAY6, FORECAST_DAY7, FORECAST_DAY8, FORECAST_DAY9, FORECAST_DAY10};
	private Context context;	// contexto
	

	public WeatherDAO(Context ctx) {
		super(ctx, DBNAME, null, DBVERSION);
		this.context = ctx;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String[] sqlLocal = context.getString(R.string.createDBLocal).split("\n");			
		db.beginTransaction();
					
		executeCommandsSQL(db, sqlLocal);
		
		db.setTransactionSuccessful();
	
		db.endTransaction();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String[] sqlDelLocal = context.getString(R.string.deleteDBLocal).split("\n");
		db.beginTransaction();
		
		executeCommandsSQL(db, sqlDelLocal);
		
		String[] sqlLocal = context.getString(R.string.createDBLocal).split("\n");			
		db.beginTransaction();
					
		executeCommandsSQL(db, sqlLocal);
		
		db.setTransactionSuccessful();
	
		db.endTransaction();
	}
	
	private void executeCommandsSQL(SQLiteDatabase db, String[] sql) {
		for( String s : sql ) {
			if (s.trim().length()>0) {	
				db.execSQL(s);
			}
		}
	}
	
	public void saveOrUpdateLocal(Local local, int action) {
		SQLiteDatabase db = getReadableDatabase();
		
		ContentValues contentLocal = new ContentValues();
		
		contentLocal.put(LOCAL_NAME, local.getName().toString()); 
				
		contentLocal.put(LOCAL_LATITUDE, local.getLatitude().toString()); 
		contentLocal.put(LOCAL_LONGITUDE, local.getLongitude().toString());
		
		contentLocal.put(FORECAST_DAY1, local.getForecast().get(0).getDay().toString());
		contentLocal.put(FORECAST_TEMPNOW1, local.getForecast().get(0).getTempNow().toString());
		contentLocal.put(FORECAST_WINDNOW1, local.getForecast().get(0).getWindNow().toString());
		contentLocal.put(FORECAST_HUMIDITYNOW1, local.getForecast().get(0).getHumidityNow().toString());
		contentLocal.put(FORECAST_RAINNOW1, local.getForecast().get(0).getRainNow().toString());
		contentLocal.put(FORECAST_WEATHER1, local.getForecast().get(0).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP1, local.getForecast().get(0).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP1, local.getForecast().get(0).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN1, local.getForecast().get(0).getRain().toString());
		contentLocal.put(FORECAST_SNOW1, local.getForecast().get(0).getSnow().toString());
		contentLocal.put(FORECAST_WIND1, local.getForecast().get(0).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY1, local.getForecast().get(0).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY2, local.getForecast().get(1).getDay().toString());
		contentLocal.put(FORECAST_WEATHER2, local.getForecast().get(1).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP2, local.getForecast().get(1).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP2, local.getForecast().get(1).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN2, local.getForecast().get(1).getRain().toString());
		contentLocal.put(FORECAST_SNOW2, local.getForecast().get(1).getSnow().toString());
		contentLocal.put(FORECAST_WIND2, local.getForecast().get(1).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY2, local.getForecast().get(1).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY3, local.getForecast().get(2).getDay().toString());
		contentLocal.put(FORECAST_WEATHER3, local.getForecast().get(2).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP3, local.getForecast().get(2).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP3, local.getForecast().get(2).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN3, local.getForecast().get(2).getRain().toString());
		contentLocal.put(FORECAST_SNOW3, local.getForecast().get(2).getSnow().toString());
		contentLocal.put(FORECAST_WIND3, local.getForecast().get(2).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY3, local.getForecast().get(2).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY4, local.getForecast().get(3).getDay().toString());
		contentLocal.put(FORECAST_WEATHER4, local.getForecast().get(3).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP4, local.getForecast().get(3).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP4, local.getForecast().get(3).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN4, local.getForecast().get(3).getRain().toString());
		contentLocal.put(FORECAST_SNOW4, local.getForecast().get(3).getSnow().toString());
		contentLocal.put(FORECAST_WIND4, local.getForecast().get(3).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY4, local.getForecast().get(3).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY5, local.getForecast().get(4).getDay().toString());
		contentLocal.put(FORECAST_WEATHER5, local.getForecast().get(4).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP5, local.getForecast().get(4).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP5, local.getForecast().get(4).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN5, local.getForecast().get(4).getRain().toString());
		contentLocal.put(FORECAST_SNOW5, local.getForecast().get(4).getSnow().toString());
		contentLocal.put(FORECAST_WIND5, local.getForecast().get(4).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY5, local.getForecast().get(4).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY6, local.getForecast().get(5).getDay().toString());
		contentLocal.put(FORECAST_WEATHER6, local.getForecast().get(5).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP6, local.getForecast().get(5).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP6, local.getForecast().get(5).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN6, local.getForecast().get(5).getRain().toString());
		contentLocal.put(FORECAST_SNOW6, local.getForecast().get(5).getSnow().toString());
		contentLocal.put(FORECAST_WIND6, local.getForecast().get(5).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY6, local.getForecast().get(5).getHumidity().toString());
				
		contentLocal.put(FORECAST_DAY7, local.getForecast().get(6).getDay().toString());
		contentLocal.put(FORECAST_WEATHER7, local.getForecast().get(6).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP7, local.getForecast().get(6).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP7, local.getForecast().get(6).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN7, local.getForecast().get(6).getRain().toString());
		contentLocal.put(FORECAST_SNOW7, local.getForecast().get(6).getSnow().toString());
		contentLocal.put(FORECAST_WIND7, local.getForecast().get(6).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY7, local.getForecast().get(6).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY8, local.getForecast().get(7).getDay().toString());
		contentLocal.put(FORECAST_WEATHER8, local.getForecast().get(7).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP8, local.getForecast().get(7).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP8, local.getForecast().get(7).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN8, local.getForecast().get(7).getRain().toString());
		contentLocal.put(FORECAST_SNOW8, local.getForecast().get(7).getSnow().toString());
		contentLocal.put(FORECAST_WIND8, local.getForecast().get(7).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY8, local.getForecast().get(7).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY9, local.getForecast().get(8).getDay().toString());
		contentLocal.put(FORECAST_WEATHER9, local.getForecast().get(8).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP9, local.getForecast().get(8).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP9, local.getForecast().get(8).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN9, local.getForecast().get(8).getRain().toString());
		contentLocal.put(FORECAST_SNOW9, local.getForecast().get(8).getSnow().toString());
		contentLocal.put(FORECAST_WIND9, local.getForecast().get(8).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY9, local.getForecast().get(8).getHumidity().toString());
		
		contentLocal.put(FORECAST_DAY10, local.getForecast().get(9).getDay().toString());
		contentLocal.put(FORECAST_WEATHER10, local.getForecast().get(9).getWeather().toString());
		contentLocal.put(FORECAST_LOWTEMP10, local.getForecast().get(9).getLowTemp().toString());
		contentLocal.put(FORECAST_HIGHTEMP10, local.getForecast().get(9).getHighTemp().toString());
		contentLocal.put(FORECAST_RAIN10, local.getForecast().get(9).getRain().toString());
		contentLocal.put(FORECAST_SNOW10, local.getForecast().get(9).getSnow().toString());
		contentLocal.put(FORECAST_WIND10, local.getForecast().get(9).getWind().toString());
		contentLocal.put(FORECAST_HUMIDITY10, local.getForecast().get(9).getHumidity().toString());
		
		if(action == 0){ 
			db.insert(DAO_TABLE_LOCAL, null, contentLocal);
		}else{
			db.update(DAO_TABLE_LOCAL, contentLocal, LOCAL_ID + "=?", new String[] {String.valueOf(local.getId())});
		}
		
		db.close();		
	}
	
	public void delete(Local local) {
		SQLiteDatabase db = getReadableDatabase();
		
		db.delete(DAO_TABLE_LOCAL,"id=" + local.getId(), null);
		
		db.close();
	}

	public int getCountlocal() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		int x;
		
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, null, null, null, null, null, null);
		x = cursor.getCount();
		
		cursor.close();
		
		db.close();
		
		return x;
	}

	public List<Local> localList() {
		
		List<Local> cities = new ArrayList<Local>();
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, null, null, null, null, null, null);
		
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			do {
				Local local = new Local();
				local.setId(cursor.getInt(0));
				local.setName(cursor.getString(1));
				local.setLatitude(cursor.getString(2)); 
				local.setLongitude(cursor.getString(3));				
				
				List<Forecast> forecast10 = new ArrayList<Forecast>();
				
				for (int i = 4; i < cursor.getColumnCount(); i = i + 8) {								
					
					Forecast forecast = new Forecast();
					
					forecast.setId(local.getId());
					forecast.setDay(cursor.getString(i));
					
					if (i == 4){
						forecast.setTempNow(cursor.getString(i + 1));
						forecast.setWindNow(cursor.getString(i + 2));
						forecast.setHumidityNow(cursor.getString(i + 3));
						forecast.setRainNow(cursor.getString(i + 4));
						i = i + 4;
					}
					
					forecast.setWeather(cursor.getString(i + 1));
					forecast.setLowTemp(cursor.getString(i + 2));
					forecast.setHighTemp(cursor.getString(i + 3));
					forecast.setRain(cursor.getString(i + 4));
					forecast.setSnow(cursor.getString(i + 5));
					forecast.setWind(cursor.getString(i + 6));
					forecast.setHumidity(cursor.getString(i + 7));										
					
					if (forecast.getDay() != null){
						forecast10.add(forecast);
					}					
				}
				
				local.setForecast(forecast10);
				
				cities.add(local);

			} while (cursor.moveToNext());
		}
		
		cursor.close();
		
		db.close();
		return cities;
	}
	
	public Local getLocal(int localId){
		
		Local local = new Local();
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, LOCAL_ID + "=?", new String[]{String.valueOf(localId)}, null, null, null, null);
		
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
							
			local.setId(cursor.getInt(0));
			local.setName(cursor.getString(1));
			local.setLatitude(cursor.getString(2)); 
			local.setLongitude(cursor.getString(3));			
			
			List<Forecast> forecast10 = new ArrayList<Forecast>();
			
			for (int i = 4; i < cursor.getColumnCount(); i = i + 8) {								
				
				Forecast forecast = new Forecast();
				
				forecast.setId(local.getId());
				forecast.setDay(cursor.getString(i));
				
				if (i == 4){
					forecast.setTempNow(cursor.getString(i + 1));
					forecast.setWindNow(cursor.getString(i + 2));
					forecast.setHumidityNow(cursor.getString(i + 3));
					forecast.setRainNow(cursor.getString(i + 4));
					i = i + 4;
				}
				
				forecast.setWeather(cursor.getString(i + 1));
				forecast.setLowTemp(cursor.getString(i + 2));
				forecast.setHighTemp(cursor.getString(i + 3));
				forecast.setRain(cursor.getString(i + 4));
				forecast.setSnow(cursor.getString(i + 5));				
				forecast.setWind(cursor.getString(i + 6));
				forecast.setHumidity(cursor.getString(i + 7));
			
				if (forecast.getDay() != null){
					forecast10.add(forecast);
				}
			}
			
			local.setForecast(forecast10);											
		}
		
		cursor.close();
		
		db.close();
		return local;	
	}
	
	public int getIdLocal(Local local) {
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		int x;
		
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, LOCAL_NAME + "=?", new String[]{local.getName()}, null, null, null, null);
		cursor.moveToFirst();
		x = cursor.getInt(0);
		
		cursor.close();
		
		db.close();
		
		return x;
	}
	
	public boolean removePastDates(String today){
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		ContentValues contentClear = new ContentValues();
		String value = null;
		
		for (int i = 0; i < FORECASTDAYS.length; i++) {
			
			cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, FORECASTDAYS[i] + "<?", new String[] {today}, null, null, null, null);
			
			cursor.moveToFirst();
			
			String column[] = getForecast(i);
			
			if (cursor.getCount() > 0){
			
				for (int j = 0; j < column.length; j++) {
					contentClear.put(column[j], value);
				}
							
				db.update(DAO_TABLE_LOCAL, contentClear, FORECASTDAYS[i] + "<?", new String[] {today});
			}
			cursor.close();
		}
			
		db.close();				
		return true;
	}
	
	public boolean isUpdated(String today){
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		boolean result = true;
			
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, FORECAST_DAY1 + "=?", new String[] {today}, null, null, null, null);
		
		cursor.moveToFirst();
		
		if (cursor.getCount() <= 0){
		
			return false;
		}
		cursor.close();
		
		db.close();				
		return result;
	}
	
	public String[] getForecast(int i){
						
		String forecast[] = null;
		
		switch (i) {
		case 0:
			forecast = FORECAST1;
			break;
		case 1:
			forecast = FORECAST2;
			break;
		case 2:
			forecast = FORECAST3;
			break;
		case 3:
			forecast = FORECAST4;
			break;
		case 4:
			forecast = FORECAST5;
			break;
		case 5:
			forecast = FORECAST6;
			break;
		case 6:
			forecast = FORECAST7;
			break;
		case 7:
			forecast = FORECAST8;
			break;
		case 8:
			forecast = FORECAST9;
			break;
		case 9:
			forecast = FORECAST10;
			break;
		default:
			break;
		}
		
		return forecast;
	}
	
	public boolean hasLocal(Local local) {
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor;
		boolean have = false;
		
		cursor = db.query(DAO_TABLE_LOCAL, COLUMNS_LOCAL, LOCAL_NAME + "=?", new String[]{local.getName()}, null, null, null, null);
		
		if(cursor.getCount() > 0){
			have = true;
		} 
		
		db.close();
		
		return have;
	}
}
